﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows.Input;
using GestionPedago.Services.ES.Modele;
using GestionPedago.Services;
using GestionPedago.Services.ES;

namespace GestionPedago.ViewModel
{
    /// <summary>
    /// Classe qui représente le gestionnaire d'onglet
    /// </summary>
    public class GestionnaireOngletsVM : BaseViewModel
    {
        /// <summary>
        /// La fenêtre principale
        /// </summary>
        public MainWindowVM vmMainWindow { get; set; }

        private ICommand closeCommand;

        /// <summary>
        /// Liste des onglets ouvert
        /// </summary>
        public ObservableCollection<OngletVM> Onglets { get; set; }

        private OngletVM _selectedOnglet;
        /// <summary>
        /// Onglet sélectionné
        /// </summary>
        public OngletVM SelectedOnglet
        {
            get
            {
                return _selectedOnglet;
            }
            set
            {
                _selectedOnglet = value;
                base.RaisePropertyChanged("SelectedOnglet");
                //vmMainWindow.vmBarreInfos.Message = string.Empty;
            }
        }

        /// <summary>
        /// Constructeur du gestionnaire d'onglets
        /// </summary>
        /// <param name="vmmainwindow">Fenêtre principale</param>
        public GestionnaireOngletsVM(MainWindowVM vmmainwindow)
        {
            vmMainWindow = vmmainwindow;
            Onglets = new ObservableCollection<OngletVM>();
        }


        public ICommand CloseCommand
        {
            get
            {
                if (this.closeCommand == null)
                    this.closeCommand = new RelayCommand(param => this.CloseTab(param));

                return this.closeCommand;
            }
        }
        /// <summary>
        /// Fermeture de l'onglet
        /// </summary>
        /// <param name="param">Onglet à fermer</param>
        public void CloseTab(object param)
        {
            OngletVM onglet = param as OngletVM;
            Onglets.Remove(onglet);
        }


        /// <summary>
        /// Mise à jour de la liste des ECFs trouvés dans l'onglet "Rechercher ECF"
        /// </summary>
        public void RefreshAfterEditECF() 
        {

            foreach(OngletVM o in Onglets) 
            {
                if(o is PlanifierECFStagiaireVM)
                    RefreshPlanifStagiaire(o);

                if(o is PlanifierECFPromotionVM)
                    RefreshPlanifPromotion(o);

                if(o is RechercheECFVM) {
                    RechercheECFVM tab = o as RechercheECFVM;
                    tab.RechercheECF();
                }
            }

        }


        /// <summary>
        /// Mises à jour après modification de la planification par promotion
        /// </summary>
        public void RefreshAfterPlanificationsPromo() {

            foreach(OngletVM o in Onglets) {
                if(o is PlanifierECFStagiaireVM)
                    RefreshPlanifStagiaire(o);

                if(o is RechercheECFVM) {
                    RechercheECFVM tab = o as RechercheECFVM;
                    tab.RechercheECF();
                }
            }
        }


        /// <summary>
        /// Mises à jour après modification de la planification par stagiaire
        /// </summary>
        public void RefreshAfterPlanificationsStagiaire() {

            foreach(OngletVM o in Onglets) {
                if(o is PlanifierECFPromotionVM) 
                    RefreshPlanifPromotion(o);

                if(o is RechercheECFVM) {
                    RechercheECFVM tab = o as RechercheECFVM;
                    tab.RechercheECF();
                }
            }
        }


        /// <summary>
        /// Rafraichir les planifications par stagiaire
        /// </summary>
        /// <param name="o">PlanifierECFStagiaireVM</param>
        private void RefreshPlanifStagiaire(OngletVM o)
        {
            if(o is PlanifierECFStagiaireVM) {
                PlanifierECFStagiaireVM tab = o as PlanifierECFStagiaireVM;
                tab.ECFs = new ObservableCollection<PlanningECFDetailES>(FacadeServices.GetInstance().RecupererPlanningECFDetail(new RecupererPlanningECFDetailIn(tab.PlaningIndivFormation)).ListePlanningECFDetail);
            }
        }


        /// <summary>
        /// Rafraichir les planifications par promotion
        /// </summary>
        /// <param name="o">PlanifierECFPromotionVM</param>
        private void RefreshPlanifPromotion(OngletVM o) {
            if(o is PlanifierECFPromotionVM) {
                PlanifierECFPromotionVM tab = o as PlanifierECFPromotionVM;
                tab.ECFs = new ObservableCollection<PlanningECFES>(FacadeServices.GetInstance().RecupererPlanningECF(new Services.ES.RecupererPlanningECFIn(tab.Promotion)).ListePlanningECF);
            }
        }
    }
}
